package com.lokyiu.mybible.db.sqlite;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.lokyiu.mybible.dao.BibleBookDao;
import com.lokyiu.mybible.db.sqlite.def.BookColumns;
import com.lokyiu.mybible.domain.BibleBook;

public class BibleBookDaoImpl implements BibleBookDao {

	private SQLiteDatabase db;
	
	public BibleBookDaoImpl(SQLiteDatabase db) {
		this.db = db;
	}
	
	@Override
	public List<BibleBook> getBooks() {
		List<BibleBook> books = new ArrayList<BibleBook>();
		Cursor cursor = db.query(BookColumns.TABLENAME, new String[] {BookColumns.BOOK_NAME},
				null, null, null, null, null, null);
		if (cursor.moveToFirst()) {
			do {
				BibleBook book = new BibleBook(cursor.getString(0), null);
				books.add(book);
			} while (cursor.moveToNext());
		}
		cursor.close();
		return books;
	}

	@Override
	public void saveBook(BibleBook book) {
		
		ContentValues values = new ContentValues();
		values.put(BookColumns.BOOK_NAME, book.getName());

		db.insert(BookColumns.TABLENAME, null, values);
	}

	@Override
	public void saveBooks(List<BibleBook> books) {
		db.beginTransaction();
		for(BibleBook book: books){
			saveBook(book);
		}
		db.setTransactionSuccessful();
		db.endTransaction();
		// TODO Auto-generated method stub
		
	}


}
